from Tkinter import *
from rulesOfTheGame import StartGame



class Game:
    def __init__(self, master):
        side = 6
        self.size = (100,100) # (y,x)
        self.running = False

        canvas = Canvas(master, height=self.size[0]*side, width=self.size[1]*side)

        for y in xrange(self.size[0]):
            for x in xrange(self.size[1]):
                box = canvas.create_rectangle(x*side, y*side, x*side+side, y*side+side, fill="#FFFFFF")

        canvas.bind("<Button-1>", self.rightMB)
        canvas.bind("<Button-3>", self.leftMB)

        canvas.pack()
        self.canvas = canvas

    def update(self, canvas):
        canvas.itemconfig(ALL, fill="#FFFFFF")
        canvas.itemconfig("clicked", fill="#00FF00")

    def refresh(self, life):
        import time
        t = time.clock()
        self.canvas.itemconfig(ALL, fill="#FFFFFF")
        for cell in life.cells:
            (x,y) = cell
            self.canvas.itemconfig(1+x+life.width*y, fill="#00FF00")

        print "time the refresh", time.clock() - t



    def rightMB(self, event): #selects a square
        canvas = event.widget

        x = canvas.canvasx(event.x)
        y = canvas.canvasx(event.y)
        
        item = canvas.find_closest(x,y)
        canvas.itemconfigure(item, tags="clicked")

        self.update(canvas)

        

    def leftMB(self, event):    # deselects a square
        canvas = event.widget

        x = canvas.canvasx(event.x)
        y = canvas.canvasx(event.y)

        item = canvas.find_closest(x,y)
        canvas.itemconfigure(item, tags="c")

        self.update(canvas)


    def start(self):
        canvas = self.canvas
        
        self.started = True

        print "starting"

        cells = []

        for box in canvas.find_withtag("clicked"):
            x = (box-1)%self.size[1]
            y = (box-1)/self.size[1]
            cells.append((x,y))



        self.life = StartGame(cells, self.size)
        self.life.run()
        status.set("The age of the cells is: %d", self.life.age)
        self.refresh(self.life)

def callback():
    print "print the callback"

def start():
    import time
    game.start()
    game.running = True

    while game.running == True:
        game.life.run()
        game.refresh(game.life)
        status.set("The age of the cells is: %d", game.life.age)
        #time.sleep(0.1)

def stop():
    game.running = False



class StatusBar(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.label = Label(self, bd=1, relief=SUNKEN, anchor=W)
        self.label.pack(fill=X)

    def set(self, format, *args):
        self.label.config(text=format % args)
        self.label.update_idletasks()

    def clear(self):
        self.label.config(text="")
        self.label.update_idletasks()


root = Tk() 

toolbar = Frame(root)

b = Button(toolbar, text="start", width=6, command=start)
b.pack(side=LEFT, padx=2, pady=2)

b = Button(toolbar, text="stop", width=6, command=stop)
b.pack(side=LEFT, padx=2, pady=2)

status = StatusBar(root)
status.pack(side=BOTTOM, fill=X)


toolbar.pack(side=TOP, fill=X)




game = Game(root)

root.mainloop()

